'use strict';

/**
 * Config for the router
 */
angular.module('app')
    .run(
        ['$rootScope', '$state', '$stateParams',
            function ($rootScope, $state, $stateParams) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]
    )
    .config(
        ['$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', 'MODULE_CONFIG', '$httpProvider',
            function ($stateProvider, $urlRouterProvider, JQ_CONFIG, MODULE_CONFIG, $httpProvider) {
                var layout = "/znyx-ncov/static/angular/tpl/app.html";

                function tagUrl(url) {
                    return '/znyx-ncov/static/angular/' + url + "?tag=" + Math.random();
                }

                $urlRouterProvider
                    .otherwise('/app/index');
                $stateProvider
                    .state('app', {
                        'abstract': true,
                        url: '/app',
                        templateUrl: layout
                    })
                    .state('app.index', {
                        url: '/index',
                        templateUrl: tagUrl('js/app/index/index.html')
                    })
                    .state('app.case_share', {
                        url: '/case_share',
                        templateUrl: tagUrl('js/app/case_share/case_share.html')
                    })
                    .state('app.online_try', {
                        url: '/online_try',
                        templateUrl: tagUrl('js/app/online_try/online_try.html')
                    });



                var interceptor = ['$location', '$q', function ($location, $q) {
                    function success(response) {

                        return response;
                    }

                    function error(response) {

                        if (response.status == 401) {
//                      window.location.href = ".";
                            return $q.reject(response);
                        } else if (response.status == 403) {
                            alert("您没有此权限，请联系管理员");
//                    $scope.addMessage("您没有此权限，请联系管理员");
                            return $q.reject(response);
                        } else {

                            return $q.reject(response);
                        }
                    }

                    return function (promise) {
                        return promise.then(success, error);
                    }
                }];

                $httpProvider.interceptors.push(interceptor);

                function load(srcs, callback) {
                    return {
                        deps: ['$ocLazyLoad', '$q',
                            function ($ocLazyLoad, $q) {
                                var deferred = $q.defer();
                                var promise = false;
                                srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
                                if (!promise) {
                                    promise = deferred.promise;
                                }
                                angular.forEach(srcs, function (src) {
                                    promise = promise.then(function () {
                                        if (JQ_CONFIG[src]) {
                                            return $ocLazyLoad.load(JQ_CONFIG[src]);
                                        }
                                        angular.forEach(MODULE_CONFIG, function (module) {
                                            if (module.name == src) {
                                                name = module.name;
                                            } else {
                                                name = src;
                                            }
                                        });
                                        return $ocLazyLoad.load(name);
                                    });
                                });
                                deferred.resolve();
                                return callback ? promise.then(function () {
                                    return callback();
                                }) : promise;
                            }]
                    }
                }


            }
        ]
    );
